<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>编写一个Chrome extension--网页二维码生成 | 四畳半神话大系 | 圈地自萌</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="">
    <meta name="description" content="很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了segmentfault的技术周刊后，决定先按照别人写过的东西去抄一遍模仿的做一遍。

本篇文章是看了从小目标开始，编写一个简洁的二维码chrome扩展模仿的。这篇文章写得很详细。我主要写写自己模仿过程中的一些问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="编写一个Chrome extension--网页二维码生成">
<meta property="og:url" content="http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/index.html">
<meta property="og:site_name" content="四畳半神话大系">
<meta property="og:description" content="很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了segmentfault的技术周刊后，决定先按照别人写过的东西去抄一遍模仿的做一遍。

本篇文章是看了从小目标开始，编写一个简洁的二维码chrome扩展模仿的。这篇文章写得很详细。我主要写写自己模仿过程中的一些问题。">
<meta property="og:image" content="http://oi6538cys.bkt.clouddn.com/chrome_extension_qrcode01.png">
<meta property="og:image" content="http://oi6538cys.bkt.clouddn.com/chrome_extension_qrcode02.png">
<meta property="og:updated_time" content="2016-12-14T14:08:18.524Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="编写一个Chrome extension--网页二维码生成">
<meta name="twitter:description" content="很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了segmentfault的技术周刊后，决定先按照别人写过的东西去抄一遍模仿的做一遍。

本篇文章是看了从小目标开始，编写一个简洁的二维码chrome扩展模仿的。这篇文章写得很详细。我主要写写自己模仿过程中的一些问题。">
<meta name="twitter:image" content="http://oi6538cys.bkt.clouddn.com/chrome_extension_qrcode01.png">
    
        <link rel="alternative" href="/atom.xml" title="四畳半神话大系" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.14">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/images/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">amoyiki</h5>
          <a href="mailto:amoyiki#gmail.com" title="amoyiki#gmail.com" class="mail">amoyiki#gmail.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/amoyiki" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-user-circle"></i>
                About
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">编写一个Chrome extension--网页二维码生成</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">编写一个Chrome extension--网页二维码生成</h1>
        <h5 class="subtitle">
            
                <time datetime="2016-12-13T14:11:33.000Z" itemprop="datePublished" class="page-time">
  2016-12-13
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/Chrome-extension/">Chrome extension</a></li></ul>

            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Chrome-extension基础"><span class="post-toc-number">1.</span> <span class="post-toc-text">Chrome extension基础</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#编写过程"><span class="post-toc-number">2.</span> <span class="post-toc-text">编写过程</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#首先编写manifest-json"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">首先编写manifest.json</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#接下来就要编写扩展弹出页面popup-html文件"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">接下来就要编写扩展弹出页面popup.html文件</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#编写相应的popup-js文件"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">编写相应的popup.js文件</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#后续"><span class="post-toc-number">3.</span> <span class="post-toc-text">后续</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#修改popup-html文件"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">修改popup.html文件</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#动态获取网页icon"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">动态获取网页icon</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-编写一个Chrome-extension-网页二维码生成"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">编写一个Chrome extension--网页二维码生成</h1>
        <div class="post-meta">
            <time class="post-time" title="2016年12月13日 22:11" datetime="2016-12-13T14:11:33.000Z"  itemprop="datePublished">2016-12-13</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/Chrome-extension/">Chrome extension</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <blockquote>
<p>很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了<code>segmentfault</code>的技术周刊后，决定先按照别人写过的东西去<del>抄一遍</del>模仿的做一遍。</p>
</blockquote>
<p>本篇文章是看了<a href="https://segmentfault.com/a/1190000007594008" target="_blank" rel="external">从小目标开始，编写一个简洁的二维码chrome扩展</a>模仿的。这篇文章写得很详细。我主要写写自己模仿过程中的一些问题。<br><a id="more"></a></p>
<h2 id="Chrome-extension基础"><a href="#Chrome-extension基础" class="headerlink" title="Chrome extension基础"></a>Chrome extension基础</h2><p>编写Chrome 扩展之前我们需要大致的了解一下Google提供的开发文档。鉴于我可怜的英文水平，我推荐花几分钟看一下下面的文档：</p>
<ol>
<li><a href="http://open.chrome.360.cn/extension_dev/overview.html" target="_blank" rel="external">360翻译的官方API文档</a></li>
<li><a href="http://www.ituring.com.cn/minibook/950" target="_blank" rel="external">Chrome扩展及应用开发</a> ←这本书不仅介绍API用法，还提供了好多实例。</li>
</ol>
<h2 id="编写过程"><a href="#编写过程" class="headerlink" title="编写过程"></a>编写过程</h2><p>有了上面几分钟的基础后，我们可以开始正式编写代码了。创建一个文件夹，将扩展所创建的文件都放在里面，方便完成后打包。</p>
<h3 id="首先编写manifest-json"><a href="#首先编写manifest-json" class="headerlink" title="首先编写manifest.json"></a>首先编写<code>manifest.json</code></h3><p>这是所有扩展的入口文件。看到后缀我们就知道这文件的语法结构必须符合json的写法。Chrome 扩展必须包含的属性有<code>name</code>、<code>version</code>、<code>manifest_version</code>。其他可选属性包括：<code>background</code>、<code>permissions</code>、<code>browser_action</code>、<code>page_action</code>、<code>options_page</code>、<code>content_scripts</code>等等。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="comment">//目前Chrome版本为2</span></span><br><span class="line">    <span class="string">"manifest_version"</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="comment">//扩展名称</span></span><br><span class="line">    <span class="string">"name"</span>: <span class="string">"QRcode"</span>,</span><br><span class="line">    <span class="comment">//扩展版本，可自定义</span></span><br><span class="line">    <span class="string">"version"</span>: <span class="string">"1.0"</span>,</span><br><span class="line">    <span class="comment">//扩展描述，显示在扩展程序中</span></span><br><span class="line">    <span class="string">"description"</span>: <span class="string">"简洁的二维码生成器"</span>,</span><br><span class="line">    <span class="comment">//显示在扩展程序中的图标</span></span><br><span class="line">    <span class="string">"icons"</span>: &#123;</span><br><span class="line">        <span class="string">"16"</span>: <span class="string">"images/icon16.png"</span>,</span><br><span class="line">        <span class="string">"128"</span>: <span class="string">"images/icon128.png"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//权限声明</span></span><br><span class="line">    <span class="string">"permissions"</span>:[<span class="string">"tabs"</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="接下来就要编写扩展弹出页面popup-html文件"><a href="#接下来就要编写扩展弹出页面popup-html文件" class="headerlink" title="接下来就要编写扩展弹出页面popup.html文件"></a>接下来就要编写扩展弹出页面<code>popup.html</code>文件</h3><p>popup页面在被用户点击时初始化，关闭后就会销毁。所以该页面更多的是用来展示结果的。数据处理则需要<code>background</code>这个属性来声明，这里暂时没用到就不多说了。需要注意的是，应该用css指定popup页面大小。另外，Google不允许HTML和JavaScript混写在同一个文件内。所有我们把相应的JS提出来，在HTML中添加外部引用。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span><span class="undefined"></span><br><span class="line">    .box &#123;</span><br><span class="line">        height: 200px;</span><br><span class="line">        width: 200px;</span><br><span class="line">        background: #EEE;</span><br><span class="line">    &#125;</span><br><span class="line">    .box .title&#123;</span><br><span class="line">        text-align: center;</span><br><span class="line">        margin-bottom: 10px;</span><br><span class="line">    &#125;</span><br><span class="line"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"box"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"title"</span>&gt;</span>扫描二维码浏览本页面<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">center</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"qrcode"</span> <span class="attr">id</span>=<span class="string">"qrcode"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">center</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"js/qrcode.js"</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"js/popup.js"</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="编写相应的popup-js文件"><a href="#编写相应的popup-js文件" class="headerlink" title="编写相应的popup.js文件"></a>编写相应的<code>popup.js</code>文件</h3><p><code>chrome.tabs</code>这个API可以与浏览器的标签页系统进行交互。具体API说明参考<a href="http://open.chrome.360.cn/extension_dev/tabs.html" target="_blank" rel="external">标签–扩展开发文档</a><br>通过获取到的标签页url传给<a href="http://code.ciaoca.com/javascript/qrcode/" target="_blank" rel="external">QRCode</a>。通过<code>QRCode.js</code>生成二维码。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">onload=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  chrome.tabs.getSelected(<span class="function"><span class="keyword">function</span>(<span class="params">tab</span>)</span>&#123;</span><br><span class="line">      <span class="comment">//QRCode(元素id,相关配置文件)</span></span><br><span class="line">      <span class="keyword">var</span> qrcode = <span class="keyword">new</span> QRCode(<span class="string">"qrcode"</span>, &#123;</span><br><span class="line">              text: tab.url,</span><br><span class="line">              width: <span class="number">160</span>,</span><br><span class="line">              height: <span class="number">160</span>,</span><br><span class="line">              colorDark : <span class="string">'#000000'</span>,</span><br><span class="line">              colorLight : <span class="string">'#ffffff'</span>,</span><br><span class="line">              <span class="comment">// QRCode的容错级别</span></span><br><span class="line">              correctLevel : QRCode.CorrectLevel.H</span><br><span class="line">            &#125;);</span><br><span class="line">    <span class="built_in">console</span>.log(qrcode);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>到目前为止，一个简单的QRCode生成器边完成了。<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oi6538cys.bkt.clouddn.com/chrome_extension_qrcode01.png" alt="加载自定义插件" title="">
                </div>
                <div class="image-caption">加载自定义插件</div>
            </figure><br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oi6538cys.bkt.clouddn.com/chrome_extension_qrcode02.png" alt="QRCode" title="">
                </div>
                <div class="image-caption">QRCode</div>
            </figure></p>
<h2 id="后续"><a href="#后续" class="headerlink" title="后续"></a>后续</h2><p>如果想让二维码中间位置显示自定义图片(如上图)，那么只需要在popup页面自定义一段CSS即可。</p>
<h3 id="修改popup-html文件"><a href="#修改popup-html文件" class="headerlink" title="修改popup.html文件"></a>修改<code>popup.html</code>文件</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span><span class="undefined"></span><br><span class="line">    .box &#123;</span><br><span class="line">        height: 200px;</span><br><span class="line">        width: 200px;</span><br><span class="line">        background: #EEE;</span><br><span class="line">        position: relative;</span><br><span class="line">    &#125;</span><br><span class="line">    .box .title&#123;</span><br><span class="line">        text-align: center;</span><br><span class="line">        margin-bottom: 10px;</span><br><span class="line">    &#125;</span><br><span class="line">    .box .qrcode&#123;</span><br><span class="line">        width: 100%;</span><br><span class="line">        height: 100%;</span><br><span class="line">        position: absolute;</span><br><span class="line">    &#125;</span><br><span class="line">    .box .logo &#123;</span><br><span class="line">          top: 80px;</span><br><span class="line">          left: 80px;</span><br><span class="line">          width: 40px;</span><br><span class="line">          height: 40px;</span><br><span class="line">          position: absolute;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"box"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"title"</span>&gt;</span>扫描二维码浏览本页面<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">center</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"qrcode"</span> <span class="attr">id</span>=<span class="string">"qrcode"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">'logo'</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"http://amoyiki.github.io/images/avatar.jpg"</span> <span class="attr">width</span>=<span class="string">"40"</span> <span class="attr">height</span>=<span class="string">"40"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">center</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"js/qrcode.js"</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"js/popup.js"</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="动态获取网页icon"><a href="#动态获取网页icon" class="headerlink" title="动态获取网页icon"></a>动态获取网页icon</h3><p>如果想让icon图标随着每个网站不同的icon进行变动的话，就只要利用<code>tab</code>的<code>favIconUrl</code>属性就能得到标签页面的图标url地址,改动如下<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">onload=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  chrome.tabs.getSelected(<span class="function"><span class="keyword">function</span>(<span class="params">tab</span>)</span>&#123;</span><br><span class="line">      <span class="comment">//QRCode(元素id,相关配置文件)</span></span><br><span class="line">      <span class="keyword">var</span> qrcode = <span class="keyword">new</span> QRCode(<span class="string">"qrcode"</span>, &#123;</span><br><span class="line">              text: tab.url,</span><br><span class="line">              width: <span class="number">160</span>,</span><br><span class="line">              height: <span class="number">160</span>,</span><br><span class="line">              colorDark : <span class="string">'#000000'</span>,</span><br><span class="line">              colorLight : <span class="string">'#ffffff'</span>,</span><br><span class="line">              <span class="comment">// QRCode的容错级别</span></span><br><span class="line">              correctLevel : QRCode.CorrectLevel.H</span><br><span class="line">            &#125;);</span><br><span class="line">    <span class="keyword">if</span> (tab.favIconUrl) &#123;<span class="comment">//tab有图标的情况下动态赋值</span></span><br><span class="line">      <span class="keyword">var</span> img = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"img"</span>)[<span class="number">1</span>].src = tab.favIconUrl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(img);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>详细代码可以查看<a href="https://github.com/amoyiki/LearnedAndProTest/tree/master/QRCode" target="_blank" rel="external">github源码地址</a></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2016-12-14T14:08:18.524Z" itemprop="dateUpdated">2016年12月14日 22:08</time>
</span><br>


        这里写留言或版权声明：<a href="/2016/12/13/编写一个Chrome-extension-网页二维码生成/" target="_blank" rel="external">http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/</a>
    </div>
    <footer>
        <a href="http://amoyiki.com">
            <img src="/images/avatar.jpg" alt="amoyiki">
            amoyiki
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            

            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&title=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&pic=http://amoyiki.com/images/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&title=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&source=
很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了segmentfault的技术周刊后，决定先按照别人写过的东西去抄一遍模仿的..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&via=http://amoyiki.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2016/12/15/git技巧-gitignore配置/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">git技巧--gitignore配置</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2016/12/12/LeetCode-290-Word-Pattern/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">[LeetCode]290. Word Pattern</h4>
      </a>
    </div>
  
</nav>



    




<section class="comments" id="comments">
    <div id="disqus_thread"></div>
    <script>
    var disqus_shortname = 'amoyiki';
    lazyScripts.push('//' + disqus_shortname + '.disqus.com/embed.js')
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</section>




</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        

        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>四畳半神话大系 &copy; 2015 - 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&title=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&pic=http://amoyiki.com/images/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&title=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&source=
很早之前就想过要用Chrome扩展开发一些实用的，或者有意思扩展。今天在看了segmentfault的技术周刊后，决定先按照别人写过的东西去抄一遍模仿的..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《编写一个Chrome extension--网页二维码生成》 — 四畳半神话大系&url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/&via=http://amoyiki.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://amoyiki.com/2016/12/13/编写一个Chrome-extension-网页二维码生成/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.4.14"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.14" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
